package com.sikao.app.ui;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.sikao.app.R;
import com.sikao.app.bean.Bean;
import com.sikao.app.bean.ReleaseEntrustSubmitBean;
import com.sikao.app.bean.UploadPicturesBean;
import com.sikao.app.dao.GraphicCodeModel;
import com.sikao.app.dao.SubmitEntrustModel;
import com.sikao.app.dao.UploadPicturesModel;
import com.sikao.app.net.OnNetBackListener;
import com.sikao.app.ui.base.BaseActivity;
import com.sikao.app.utils.JSONUtil;
import com.sikao.app.utils.StringUtils;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * 发布委托
 */
public class ReleaseEntrustActivity extends BaseActivity implements View.OnClickListener {

	private TextView titleTv;
	private ImageView backIv;
	private EditText phoneEt;
	private EditText imgCodeEt;
	private ImageView imgCodeIv;
	private EditText titleEt;
	private EditText contentEt;
	private RelativeLayout typeRl;
	private TextView typeTv;
	private ImageView ivImg;
	private Button replyBtn;
	private SubmitEntrustModel submitEntrustModel;
	private ReleaseEntrustSubmitBean bean;
	private String[] types = {"出售","采购","委托进口","全委托"};
	private AlertDialog dialog;

	private String picturePath;
	private UploadPicturesModel uploadPicturesModel;
	private String uploadPicturePath;
	public static final int REQUEST_GET_PHOTO = 17;

	private int type = 0;//1代表委托销售，2代表委托采购

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_release_entrust);
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void initData() {
	}

	@Override
	protected void initView() {
		titleTv = (TextView) findViewById(R.id.title_middle_tv);
		backIv = (ImageView) findViewById(R.id.title_left_img);
		phoneEt = (EditText) findViewById(R.id.phone_et);
		imgCodeEt = (EditText) findViewById(R.id.img_code_et);
		imgCodeIv = (ImageView) findViewById(R.id.img_code_iv);
		titleEt = (EditText) findViewById(R.id.title_et);
		contentEt = (EditText) findViewById(R.id.content_et);
		typeRl = (RelativeLayout) findViewById(R.id.type_rl);
		typeTv = (TextView) findViewById(R.id.type_tv);
		ivImg = (ImageView) findViewById(R.id.iv_img);
		replyBtn = (Button) findViewById(R.id.reply_btn);
	}

	@Override
	protected void initListener() {
		backIv.setOnClickListener(this);
		replyBtn.setOnClickListener(this);
		typeRl.setOnClickListener(this);
		ivImg.setOnClickListener(this);
		imgCodeIv.setOnClickListener(this);
	}

	@Override
	protected void init() {
		titleTv.setText("委托业务");
		backIv.setVisibility(View.VISIBLE);
		typeTv.setText("出售");
		getGraphicCode();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.title_left_img:
				finish();
				break;
			case R.id.img_code_iv:
				getGraphicCode();
				break;
			case R.id.iv_img:
				Intent intent1 = new Intent(ReleaseEntrustActivity.this, SelectPictureActivity.class);
				startActivityForResult(intent1, REQUEST_GET_PHOTO);
				overridePendingTransition(R.anim.bottom_push_in, R.anim.bottom_push_out);
				break;
			case R.id.type_rl:
				chooseType();
				break;
			case R.id.reply_btn:
				submitData();
				break;
			default:
				break;
		}
	}

	private void chooseType() {
		if (dialog == null) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			View view = View.inflate(this, R.layout.listview, null);
			builder.setView(view);
			ListView listView = (ListView) view.findViewById(R.id.listview);
			listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, types));
			listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					if (position == 0) {
						type = 0;
						typeTv.setText("出售");
						dialog.dismiss();
					}
					if (position == 1) {
						type = 1;
						typeTv.setText("采购");
						dialog.dismiss();
					}

					if (position == 2) {
						type = 2;//待修改
						typeTv.setText("委托进口");
						dialog.dismiss();
					}
					if (position == 3) {
						type = 3;//待修改
						typeTv.setText("全委托");
						dialog.dismiss();
					}

				}
			});
			dialog = builder.create();
		}
		dialog.show();
	}

	private boolean checkInput() {

		if (TextUtils.isEmpty(titleEt.getText().toString())) {
			Toast.makeText(this, "请输入标题", Toast.LENGTH_SHORT).show();
			return false;
		}
		if (TextUtils.isEmpty(contentEt.getText().toString())) {
			Toast.makeText(this, "请输入内容", Toast.LENGTH_SHORT).show();
			return false;
		}
		if (TextUtils.isEmpty(phoneEt.getText().toString())) {
			Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
			return false;
		}
		/*if (!StringUtils.isMobile(phoneEt.getText().toString())) {
			Toast.makeText(this, "手机号格式错误", Toast.LENGTH_SHORT).show();
			return false;
		}*/
		return true;
	}

	private void submitData() {
		if (!checkInput()) {
			return;
		}
		if (submitEntrustModel == null) {
			bean = new ReleaseEntrustSubmitBean();
			submitEntrustModel = new SubmitEntrustModel(this, new OnNetBackListener<Bean>() {
				@Override
				public void netBack(Bean data) {
					if (data == null) {
						return;
					}
					if (data.getStatus().equals("1")) {
						Toast.makeText(ReleaseEntrustActivity.this,  data.getMsg(), Toast.LENGTH_SHORT).show();
						finish();
					} else {
						getGraphicCode();
						Toast.makeText(ReleaseEntrustActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
					}
				}
			});
		}
		bean.setName(titleEt.getText().toString());
		bean.setCont(contentEt.getText().toString());

		switch (type){
			case 0:
				bean.setType("sell");
				break;
			case 1:
				bean.setType("buy");
				break;
			case 2:
				bean.setType("imports");
				break;
			case 3:
				bean.setType("allsad");
				break;
		}



		bean.setPhone(phoneEt.getText().toString());
		bean.setProfilePic(uploadPicturePath);
		bean.setImgType("addSad");
		bean.setImgCode(imgCodeEt.getText().toString());
		bean.setImgTypeId("1");
		submitEntrustModel.request(JSONUtil.toJSON(bean), true);
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
				case REQUEST_GET_PHOTO:
					picturePath = data.getStringExtra("path");
					uploadPicture(picturePath);
					break;
				default:
					break;
			}
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (submitEntrustModel != null) {
			submitEntrustModel.cancelRequest();
		}
	}

	private void getGraphicCode() {
		new GraphicCodeModel(this, new OnNetBackListener<Bitmap>() {
			@Override
			public void netBack(Bitmap bm) {
				if (bm != null) {
					imgCodeIv.setImageBitmap(bm);
				} else {
					imgCodeIv.setImageDrawable(getResources().getDrawable(R.mipmap.img_code_fail));
				}
			}
		}).request("addSad", false);
	}

	private void uploadPicture(final String picturePath) {
		if (uploadPicturesModel == null) {
			uploadPicturesModel = new UploadPicturesModel(ReleaseEntrustActivity.this, new OnNetBackListener<UploadPicturesBean>() {
				@Override
				public void netBack(UploadPicturesBean data) {
					if (data == null) {
						Toast.makeText(ReleaseEntrustActivity.this, "上传失败", Toast.LENGTH_SHORT).show();
						return;
					}
					if (data.getStatus().equals("1")) {
						uploadPicturePath = data.getRes().getArr().get(0).getId();
						ImageLoader.getInstance().displayImage("file://" + picturePath, ivImg);
						//ImageLoader.getInstance().displayImage(String.valueOf(Uri.fromFile(new File(picturePath))), ivImg);
					} else {
						Toast.makeText(ReleaseEntrustActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
					}
				}
			});
		}
		Map<String, String> map = new HashMap<>();
		map.put("picture.png", picturePath);
		uploadPicturesModel.request(map, true);
	}
}
